import { UserInfo } from "../common/UserInfo";
import { EventManager } from "../event/EventManager";
import { EventType, sAdvanced, soldierGrade } from "../settings/const";
import { Building } from "./Buildings";
import { Events } from "./Events";
import { Soldier } from "./Soldier";

export enum UpGradeType {
     Attack = 1,
     Defense = 2

}

/**士兵升级数据对象 */
export class Advanced {
     /**士兵 */
     soldier: Soldier;
     /**建筑物，与升级所需时间挂钩 */
     building: Building;
     /**当前的等级， */
     level: number;
     /**最大等级， */
     maxLevel: number;
     /**攻击力或者防御力的加成 */
     addition: number;
     /**攻击力或者防御力的，下一级加成 */
     additionNext: number
     /**木材 */
     wood: number;
     /**石矿 */
     mud: number;
     /**铁矿 */
     iron: number;
     /**粮食 */
     food: number;
     /**升级所需的时间列表，其跟建筑物等级挂钩 */
     timeListWidthBuilding: number[];
     /**升级所需的时间 */
     time: number = 0;
     /**升级类型（攻击、防御） */
     type: UpGradeType;
     /**
      * 获取士兵升级，所需要的资源和时间
      * @param soldier 士兵对象
      * @param level    当前等级（攻击等级或者防御等级）
      * @param type  升级类型（攻击、防御）
      * @param buildingLevel 建筑物等级（铁匠铺或者军械库）
      * @returns 
      */
     static createBySoldierAndLevel(soldier: Soldier, level: number, type: UpGradeType, building: Building): Advanced {
          if(level == 0){
               level = 1
          }
          
          let ad = new Advanced()
          ad.soldier = soldier
          ad.building = building

          ad.level = level
          ad.maxLevel = sAdvanced[soldier.id].length
          ad.type = type
          
          ad.addition = soldierGrade[level] - 1
          ad.additionNext = soldierGrade[level + 1] - 1

          let data = sAdvanced[soldier.id][level-1]

          ad.wood = data[1]
          ad.mud = data[2]
          ad.iron = data[3]
          ad.food = data[4]
          ad.time = Math.floor(data[5] / UserInfo.instance.speed) 


          // ad.timeListWidthBuilding = data.slice(5)
          // if (building.level == 0) {
          //      ad.time = ad.timeListWidthBuilding[0]
          // } else {
          //      ad.time = ad.timeListWidthBuilding[building.level - 1]
          // }

          return ad
     }

     /**
      * 根据id 和 等级 来创建
      * @param id 
      * @param level 
      * @returns 
      */
     static createByIdAndLevel(id:number, level:number): Advanced{
          let ad = new Advanced()
          ad.level = level
          ad.maxLevel = sAdvanced[id].length
          let data = sAdvanced[id][level-1]
          ad.wood = data[1]
          ad.mud = data[2]
          ad.iron = data[3]
          ad.food = data[4]
          ad.time = Math.floor(data[5] / UserInfo.instance.speed) 
          return ad
     }

     /**
      * 判断是否满足升级条件
      * @returns 
      */
     get isCanUpGrade(): boolean {

          if (!this.isEnoughBuilding) {
               return false
          }

          if (!this.isEnoughRes) {
               return false
          }

          return true
     }

     /**
      * 判断建筑物等级是否够
      */
     get isEnoughBuilding(): boolean {

          if (this.level >= this.building.level) {
               return false
          }

          return true
     }

     /**
      * 判断是否有足够的资源
      */
     get isEnoughRes(): boolean {
          let villageData = UserInfo.instance.villageData
          let food = villageData.res.food_reserve
          let iron = villageData.res.iron_reserve
          let wood = villageData.res.wood_reserve
          let mud = villageData.res.mud_reserve

          if (food < this.food) {
               return false
          }
          if (iron < this.iron) {
               return false
          }
          if (wood < this.wood) {
               return false
          }
          if (mud < this.mud) {
               return false
          }
          return true
     }

     /**
      * 获取升级事件对象
      * @returns 
      */
     getEvent(): Events {
          let eventList = UserInfo.instance.eventList

          for (let e of eventList) {
               if (e.eType == EventType.advSoldier) {
                    let arr = e.eParam.split(',')
                    let id = parseInt(arr[0])
                    let type = parseInt(arr[1]) + 1
                    if (id == this.soldier.id && type == this.type) {
                         return e
                    }
               }


          }

          return null
     }

     /**
      * 升级倒计时完成后调用
      */
     upGradeComplete() {
          let event = this.getEvent()
          UserInfo.instance.removeEvent(event)

          let soldierList = Soldier.createBySoldiersString()

          let tempSo: Soldier
          for (let so of soldierList) {
               if (so.id == this.soldier.id) {
                    tempSo = so
               }
          }

          if (this.type == UpGradeType.Attack) {
               tempSo.attackLevel++
          }

          if (this.type == UpGradeType.Defense) {
               tempSo.defenseLevel++
          }

          let list = soldierList.map(so => {
               let arr = [
                    this.soldier.id,
                    this.soldier.count,
                    this.soldier.attackLevel,
                    this.soldier.defenseLevel
               ]
               return arr.join(',')
          })

          UserInfo.instance.villageData.soldiersString = list.join(';')
          console.log(UserInfo.instance.villageData.soldiersString)
          EventManager.instance.emit(EventManager.EventType.AdvancedComplete, this.soldier)
     }
}


